﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class SysManage_BDBLoginSet : INTPage
{
    public int rowNo = 0;
    CommonMethod comHelp = new CommonMethod();
    DBLoginSetDAO com = new DBLoginSetDAO();
    private string errorTimeOutUrl = "../error.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, errorTimeOutUrl);
        if (!IsPostBack)
        {
            //页面权限
            comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "dbLoginSet" },
                Grv, new string[] { "cmdAdd", "cmdEdit" }, new int[] { Grv.Columns.Count - 2, Grv.Columns.Count - 1 }, CommonModel.loginOut1);

            bindDBLoginSet();                      
        }
    }
    //绑定
    public void bindDBLoginSet()
    {
        Grv.DataSource = com.GetDBLoginSet();
        Grv.DataBind();
    }
    // 换页
    protected void Grv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            Grv.PageIndex = e.NewPageIndex;
            bindDBLoginSet();
        }
        catch
        {
        }
    }
    protected void Grv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        rowNo++;
        //鼠标经过事件
        if (e.Row.RowIndex != -1)
        {
            //设置悬浮鼠标指针形状为"小手"
            e.Row.Attributes["style"] = "Cursor:hand";
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "lastColor=this.style.backgroundColor;this.style.backgroundColor='#CCCC99';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=lastColor;");
                if ("zh-cn".Equals(Session["Language"].ToString()))
                {
                    ((LinkButton)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0]).Attributes.Add("onclick", "return confirm('确实要删除此DB？');");
                }
                else
                {
                    ((LinkButton)e.Row.Cells[e.Row.Cells.Count - 1].Controls[0]).Attributes.Add("onclick", "return confirm('OK to delete?');");
                }
            }
        }
    }

    // 添加
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, errorTimeOutUrl);

        DBLoginSetModel dbLoginSet = new DBLoginSetModel();
        string userCode = Session["UserCode"].ToString();
        string strDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
        dbLoginSet.DBCode = txtCode.Text.ToString();
        dbLoginSet.DBServerName = txtServerName.Text.ToString();
        dbLoginSet.DBServerIP = txtServerIP.Text.ToString();
        dbLoginSet.DBName = txtDBName.Text.ToString();
        dbLoginSet.DBLoginName = txtLoginName.Text.ToString();
        dbLoginSet.DBLoginPwd = txtLoginPwd.Text.ToString();
        dbLoginSet.DBDesc = txtDesc.Text.ToString();
        dbLoginSet.DeleteFlag = "0";
        dbLoginSet.AddAuthor = userCode;
        dbLoginSet.AddDate = strDate;
        dbLoginSet.UpdateAuthor = userCode;
        dbLoginSet.UpdateDate = strDate;
        dbLoginSet.DBForFlag = ddlDBForFlag.SelectedValue;
        dbLoginSet.DBType = Convert.ToInt16(ddlDBType.SelectedValue);

        if (com.AddDBLoginSet(dbLoginSet) > 0)
        {
            CommonMethod.regAlertMessage("操作成功-Success!", this);
            bindDBLoginSet();
            SetIfEdit(false);
        }
        else
        {
            CommonMethod.regAlertMessage("出现错误-Error!", this);
        }
    }
    // 编辑
    protected void cmdEdit_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCode(this, errorTimeOutUrl);

        DBLoginSetModel dbLoginSet = new DBLoginSetModel();
        string userCode = Session["UserCode"].ToString();
        string strDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
        dbLoginSet.DBCode = txtCode.Text.ToString();
        dbLoginSet.DBServerName = txtServerName.Text.ToString();
        dbLoginSet.DBServerIP = txtServerIP.Text.ToString();
        dbLoginSet.DBName = txtDBName.Text.ToString();
        dbLoginSet.DBLoginName = txtLoginName.Text.ToString();
        dbLoginSet.DBLoginPwd = txtLoginPwd.Text.ToString();
        dbLoginSet.DBDesc = txtDesc.Text.ToString();
        dbLoginSet.UpdateAuthor = userCode;
        dbLoginSet.UpdateDate = strDate;
        dbLoginSet.DBForFlag = ddlDBForFlag.SelectedValue;
        dbLoginSet.DBType = Convert.ToInt16(ddlDBType.SelectedValue);

        if (com.EditDBLoginSet(dbLoginSet) > 0)
        {
            CommonMethod.regAlertMessage("操作成功-Success!", this);
            bindDBLoginSet();
            SetIfEdit(false);
        }
        else
        {
            CommonMethod.regAlertMessage("出现错误-Error!", this);
        }
    }
    // 删除
    protected void Grv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DBLoginSetModel dbLoginSet = new DBLoginSetModel();
        dbLoginSet.DBCode = Grv.Rows[e.RowIndex].Cells[1].Text;

        if (com.DeleteDBLoginSet(dbLoginSet) > 0)
        {
            CommonMethod.regAlertMessage("操作成功-Success!", this);
            bindDBLoginSet();
            SetIfEdit(false);
        }
        else
        {
            CommonMethod.regAlertMessage("无法删除-Disable Delete", this);
        }
    }
    // 编辑绑定
    protected void Grv_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtCode.Text = Grv.Rows[Grv.SelectedIndex].Cells[1].Text;
        txtServerName.Text = Grv.Rows[Grv.SelectedIndex].Cells[2].Text;
        txtServerIP.Text = Grv.Rows[Grv.SelectedIndex].Cells[3].Text;
        txtDBName.Text = Grv.Rows[Grv.SelectedIndex].Cells[4].Text;
        txtLoginName.Text = Grv.Rows[Grv.SelectedIndex].Cells[5].Text;
        txtLoginPwd.Text = Grv.DataKeys[Grv.SelectedIndex]["DBLoginPwd"].ToString();
        ddlDBType.SelectedValue = Grv.DataKeys[Grv.SelectedIndex]["DBType"].ToString();
        ddlDBForFlag.SelectedValue = Grv.DataKeys[Grv.SelectedIndex]["DBForFlag"].ToString();

        txtDesc.Text = Grv.Rows[Grv.SelectedIndex].Cells[Grv.Columns.Count-2].Text;

        SetIfEdit(true);
    }
    protected void cmdReset_Click(object sender, EventArgs e)
    {
        SetIfEdit(false);
    }
    
    private void SetIfEdit(bool isEdit)
    {
        cmdAdd.Enabled = !isEdit;
        cmdEdit.Enabled = isEdit;
        txtCode.ReadOnly = isEdit;
        if (!isEdit)
        {
            txtDesc.Text = string.Empty;
            txtCode.Text = string.Empty;
            txtServerName.Text = string.Empty;
            txtServerIP.Text = string.Empty;
            txtLoginName.Text = string.Empty;
            txtLoginPwd.Text = string.Empty;
            txtDBName.Text = string.Empty;
        }
    }
}